<template>
    <div class="x-scroll" v-obElementSize="handleElSizeChange">
        <div class="vertical-box">
            <div class="content">
                <slot></slot>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";

const xScroll = reactive({
    w: 0,
    h: 0
});
const handleElSizeChange = (rect: DOMRectReadOnly) => {
    xScroll.w = rect.width;
    xScroll.h = rect.height;
};
</script>

<style scoped lang="scss">
.x-scroll {
    width: 100%;
    height: 100%;
    .vertical-box {
        position: relative;

        /* 宽高和容器盒子相反，营造容器盒子垂直的样子 */
        width: v-bind("xScroll.h + 'px'");
        height: v-bind("xScroll.w + 'px'");

        /* 实现内容盒子元素的滚动 */
        overflow: auto;

        /* 垂直盒子向上旋转，并向下平移和容器盒子重合 */
        transform: rotate(-90deg) translate(-100%, 0);
        transform-origin: left top;

        /* 隐藏滚动条 */
        &::-webkit-scrollbar {
            display: none;
        }
        .content {
            /* 内容盒子左边刚好距离容器盒子一个高度的距离 */
            position: absolute;
            left: 100%;

            /* 保证内容盒子旋转后垂直盒子重合 */
            height: v-bind("xScroll.h + 'px'");

            /* 向下旋转和垂直盒子重合 */
            transform: rotate(90deg);
            transform-origin: left top;
        }
    }
}
</style>
